
using System;
using System.Threading;
using System.Windows.Browser;
using Qdb.Tests.Common.References;
using QdbUnit;
using QdbUnit.Extensions;
using QdbUnit.Extensions.Fixtures;

namespace Qdb.Silverlight.TestStart
{
	public partial class MainPage
	{
		public MainPage()
		{
			InitializeComponent();
			ThreadPool.QueueUserWorkItem(unused => TestRunnerEntry());
		}

		private void TestRunnerEntry()
		{
			try
			{
				Type[] testCases = new[]
				                   	{
				                   		typeof(Tests.Common.AllTests), 
										typeof(Tests.CLI1.AllTests),
										typeof(Tests.CLI2.AllTests),
				                   	};

				new TestRunner(SilverlightSuite(testCases)).Run(new SilverlightTestListener(Dispatcher));

				Complete();
			}
			catch(Exception ex)
			{
				AppendException(ex);
			}
		}

		private void Complete()
		{
			Dispatcher.BeginInvoke(() => HtmlPage.Window.Eval("completed();"));
		}

		private void AppendException(Exception exception)
		{
			Dispatcher.BeginInvoke(() => HtmlPage.Window.Eval("appendException(\"" + exception.ToJScriptString() + "\");"));
		}

		private static QdbTestSuiteBuilder SilverlightSuite(params Type[] testCases)
		{
			return new QdbTestSuiteBuilder(new SilverlightFixture(), testCases);
		}
	}
}
